Page({
  data: {
    username: '',
    verificationCode: '',
    password: '',
    countdown: ''
  },

  onPhoneChange(e) {
    this.setData({
      username: e.detail
    });
    console.log('username', this.data.username);
  },

  onCodeChange(e) {
    this.setData({
      verificationCode: e.detail
    });
    console.log('验证码', this.data.verificationCode);
  },

  sendSmsCode() {
    const {
      username,
      countdown
    } = this.data;
    if (!username) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }

    // 简单的手机号格式验证
    if (!/^1[3-9]\d{9}$/.test(username)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      });
      return;
    }

    // 调用后端接口发送验证码
    wx.request({
      url: 'http://localhost:8080/phoneCaptcha',
      method: 'GET',
      data: {
        phone: username
      },
      success: (res) => {
        console.log("获取的数据");
        console.log(res);
        if (res.data.code === 200) {
          console.log(res.data.data);
          wx.showToast({
            title: '验证码已发送，请注意查收, 验证码为: ' + res.data.data,
            icon: 'none'
          });
        } else if (res.data.code === 500) {
          wx.showToast({
            title: '验证码已发送，请稍后再试',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: res.data.message || '发送失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误，请稍后再试',
          icon: 'none'
        });
      }
    });
  },

  login() {
    const {
      username,
      password,
      verificationCode,
      countdown
    } = this.data;

    if (!username) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      });
      return;
    }

    console.log("fa", countdown);
    if (!verificationCode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }

    const app = getApp();

    // 模拟登录请求
    wx.request({
      url: 'http://localhost:8080/wx/login',
      method: 'POST',
      data: {
        username: username,
        code: verificationCode
      },
      success: (res) => {
        console.log("登录后获取到的数据");
        console.log(res.data);
        let token = {
          token: res.data.token
        }
        app.setUserInfo(token)
        // 存储 Token 到本地
        wx.setStorageSync('token', res.data.token);
        if (res.data.code === 200) {
          wx.request({
            url: 'http://localhost:8080/wx/login/userinfo',
            method: 'GET',
            header: {
              Authorization: res.data.token
            },
            success: (response) => {
              console.log("获取用户信息");
              console.log(response.data);
              let userInfo = response.data.data
              app.setUserInfo(userInfo)
              
              // 存储用户信息到本地
              wx.setStorageSync('user', userInfo);
              
              // 检查是否有返回路径
              const returnUrl = wx.getStorageSync('returnUrl');
              if (returnUrl) {
                // 有返回路径则跳转回原页面
                wx.redirectTo({ url: returnUrl });
                // 清除返回路径
                wx.removeStorageSync('returnUrl');
              } else {
                // 无返回路径则跳转至默认页面
                wx.showToast({
                  title: '老板登录了，欢迎老板',
                });
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg || '登录失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('登录请求失败:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  }
});